#include "StringConverter.h"

#include <algorithm>
#include <sstream>

StringConverter::StringConverter(void)
{
}


StringConverter::~StringConverter(void)
{
}

bool StringConverter::convertToBool(const std::string& value)
{
	if (value == "1" || value == "true" || value == "yes")	{	return true;	}
	return false;
}

int StringConverter::boolToInt(const std::string& value)
{
	if (value == "true" || value == "false")
	{
		if(value == "true")
		{
			return 1;
		}
		if(value == "false")
		{
			return 0;
		}
	}
	return -1;
}

int StringConverter::convertToInt(const std::string& value)
{
	if(value == "true" || value == "false")
	{
		return boolToInt(value);
	}

	int returnValue = 0;

	std::istringstream stream(value);
	stream >> returnValue;

	return returnValue;
}

float StringConverter::convertToFloat(const std::string& value)
{
	float returnValue = 0.0f;

	std::istringstream stream(value);
	stream >> returnValue;

	return returnValue;
}

std::vector<std::string> StringConverter::convertToStringVector(const std::string& value, const std::string& delimiter)
{
	std::vector<std::string> values;
	size_t startPos = 0;
	size_t currentPos = 0;

	do
	{
		currentPos = value.find_first_of(delimiter, startPos);
		if (currentPos == startPos)
		{
			startPos = currentPos + 1;
		}
		else if (currentPos == std::string::npos)
		{
			values.push_back( value.substr(startPos) );
			break;
		}
		else
		{
			std::string subStr = value.substr(startPos, currentPos - startPos);
			values.push_back(subStr);
			startPos = currentPos + 1;
		}

		startPos = value.find_first_not_of(delimiter, startPos);

	} while (currentPos != std::string::npos);

	return values;
}

std::string StringConverter::toString(bool value, bool getAsYesNo)
{
	if ( value )
	{
		if ( getAsYesNo )	{	return "yes";	}
		else				{	return "true";	}
	}
	else
	{
		if ( getAsYesNo )	{	return "no";	}
		else				{	return "false";	}
	}
}

std::string StringConverter::toString(int value, unsigned short width, char fill, std::ios::fmtflags flags)
{
	std::stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags) { stream.setf(flags); }
	stream << value;

	return stream.str();
}

std::string StringConverter::toString(float value, unsigned short width, char fill, std::ios::fmtflags flags)
{
	std::stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags) { stream.setf(flags); }
	stream << value;

	return stream.str();
}

void StringConverter::toLowerCase(std::string& value)
{
	std::transform(value.begin(), value.end(), value.begin(), ::tolower);
}

void StringConverter::toUpperCase(std::string& value)
{
	std::transform(value.begin(), value.end(), value.begin(), ::toupper);
}

bool StringConverter::isNumber(const std::string& value)
{
	std::stringstream stream(value);
	float test = 0.0f;
	stream >> test;

	return (!stream.fail() && stream.eof());
}